package com.mediacloud.app.newsmodule.fragment.video.vod

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.mediacloud.app.assembly.util.GlideUtils
import com.mediacloud.app.assembly.util.load
import com.mediacloud.app.assembly.views.GlideRoundTransform
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapterX
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig
import org.json.JSONObject

/**
Date:2020/4/21
Author:Wy
 */
class MovieListAdapter(context: Context?) : BaseRecyclerAdapterX<ArticleItem>(context) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return MovieHolder(inflate(R.layout.movie_grid_item, parent), context)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        if (holder is MovieHolder) {
            holder.setBaseNewsItemData(getItem(position), position)
        }
    }

}

class MovieHolder(itemView: View, context: Context) : RecyclerView.ViewHolder(itemView) {
    var context = context;
    var trans = GlideRoundTransform(itemView.context, itemView.resources.getDimension(R.dimen.dimen5))
    var logo: ImageView = itemView.findViewById(R.id.logo)
    var title: TextView = itemView.findViewById(R.id.title)
    var text_desc: TextView = itemView.findViewById(R.id.text_desc)
    var layout_content: ConstraintLayout = itemView.findViewById(R.id.layout_content)

    val dimen15 = itemView.context.resources.getDimensionPixelSize(R.dimen.dimen15)
    val dimen7 = itemView.context.resources.getDimensionPixelSize(R.dimen.dimen7)
    val dimen5 = itemView.context.resources.getDimensionPixelSize(R.dimen.dimen5)

    fun setBaseNewsItemData(articleItem: ArticleItem, position: Int) {

        //调整下间距
        when ((position + 1) % 3) {
            0 -> {
                layout_content.setPadding(0, dimen5, dimen15, dimen5)
            }
            1 -> {
                layout_content.setPadding(dimen15, dimen5, 0, dimen5)
            }
            2 -> {
                layout_content.setPadding(dimen7, dimen5, dimen7, dimen5)
            }
        }

        var appCustomParams = articleItem.appCustomParams

        if (!TextUtils.isEmpty(appCustomParams)) {
            val paramObj = JSONObject(appCustomParams)
            val obj = paramObj.optJSONObject("movie")
            val logoStr = obj?.optString("verticalLogo")
            val summary = obj?.optString("aliasName")
            if (!TextUtils.isEmpty(logoStr)) {
                logo.load(logoStr,dimen5)

//                GlideUtils.loadUrl(logoStr, logo, null, AppFactoryGlobalConfig.getDefaultImageLoadDrawable(), true, false, trans)
            } else {
                logo.load(articleItem.logo,dimen5)
//                GlideUtils.loadUrl(articleItem.logo, logo, null, AppFactoryGlobalConfig.getDefaultImageLoadDrawable(), true, false, trans)
            }
            if (!TextUtils.isEmpty(summary)) {
                text_desc.text = summary
            } else {
                text_desc.text = articleItem.summary
            }
        } else {
            logo.load(articleItem.logo,dimen5)
//            GlideUtils.loadUrl(articleItem.logo, logo, null, AppFactoryGlobalConfig.getDefaultImageLoadDrawable(), true, false, trans)
            text_desc.text = articleItem.summary
        }


        title.text = articleItem.title

    }

}